<?php
include('SEGErros.php');
class SEGMysql {
    private $db, $conn, $valida;
	public $status, $numReg, $erro;
	
    public function __construct($server="localhost", $database="erp_controle", $username="root", $password="vertrigo"){
        $this->conn = mysql_connect($server, $username, $password);
        $this->db   = mysql_select_db($database,$this->conn);
		$this->valida = new SEGErros();
    }
	
	public function getSql($sql){	
        $result = $this->sqlordie($sql);
		if ($this->status > 0) {
			$this->numReg = mysql_num_rows($result);
			return mysql_fetch_assoc($result);
		} else
			return '';
    }

    public function insert_array($table, $insert_values) {
        foreach($insert_values as $key=>$value) {
            $keys[] = $key;
            $insertvalues[] = '\''.$value.'\'';
        }
        $keys = implode(',', $keys);
        $insertvalues = implode(',', $insertvalues);
        $sql = "INSERT INTO $table ($keys) VALUES ($insertvalues)";
        $this->sqlordie($sql);
    }

    public function update_array($table, $keyColumnName, $id, $update_values) {
        foreach($update_values as $key=>$value) {
            $sets[] = $key.'=\''.$value.'\'';
        }
        $sets = implode(',', $sets);
        $sql = "UPDATE $table SET $sets WHERE $keyColumnName = '$id'";
        $this->sqlordie($sql);
    }

	public function getSelect($table, $fields = "*",$where = ""){
        $sql = "SELECT $fields FROM $table ".(($where != "") ? "WHERE ".$where : "");
		
        $result = $this->sqlordie($sql);
		if ($this->status > 0) {
			$this->numReg = mysql_num_rows($result);
			$obj = array();
        	while($reg = mysql_fetch_assoc($result)) {
				array_push($obj,$reg); 
			}			
			return $obj;
		} else
			return '';
    }
	
    public function getSelectById($table, $keyColumnName, $id, $fields = "*"){
        $sql = "SELECT $fields FROM $table WHERE $keyColumnName = '$id'";		
        $result = $this->sqlordie($sql);
        if ($this->status > 0) {
			$this->numReg = mysql_num_rows($result);
        	$obj = mysql_fetch_assoc($result);
			$obj = array($table=>$obj);
			return $this->valida->msg($obj,'J');
		} else
			return '';
    }

    public function getSelectByGroup($table, $groupKeyName, $groupID, $orderKeyName = '', $order = 'ASC', $fields = '*'){
        $orderSql = '';
        if($orderKeyName != '') $orderSql = " ORDER BY $orderKeyName $order";
        $sql = "SELECT * FROM $table WHERE $groupKeyName = '$groupID'" . $orderSql;

        $result = $this->sqlordie($sql);

        while($row = mysql_fetch_assoc($result)) {
            $records[] = $row;
        }
        return $records;
    }

    private function sqlordie($sql) {       
        $return_result = mysql_query($sql, $this->conn);
        if($return_result) {
			$this->status = 1;
            return $return_result;
        } else {
			$this->status = 0;
            $this->sql_error($sql);
        }
    }

    private function sql_error($sql) {		
		$this->erro = $this->valida->msg('Erro - '. mysql_error($this->conn) . ' - ' . $sql);
    }
}
?>